﻿using System;
using System.Collections.Generic;
using Protocol.Code;
using Protocol.Constant;
using Protocol.Dto.Fight;
using Script.Audio;
using Script.Character;
using Script.Framework;
using Script.Model;
using Script.UI;
using Script.UI.Msg;
using UnityEngine;

namespace Script.Net.Impl
{
    public class FightHandler : HandlerBase
    {
        // Flag
        private bool isFirstGrab = true; // 是否是第一个玩家抢地主


        public override void OnReceive(int subCode, object value)
        {
            switch (subCode)
            {
                case FightCode.GET_CARD_SRES:
                    GetCards(value as List<CardDto>);
                    break;
                case FightCode.TURN_GRAB_BRQ:
                    turnGrabBro((int) value);
                    break;
                case FightCode.GRAB_LANDLORD_BRQ:
                    grabLandLordBro(value as GrabDto);
                    break;
                case FightCode.TURN_DEAL_BRQ:
                    TurnDealBro((int) value);
                    break;
                case FightCode.DEAL_BRQ:
                    DealBro(value as DealDto);
                    break;
                case FightCode.DEAL_SRES:
                    dealResponse((int) value);
                    break;
                case FightCode.OVER_BRQ:
                    overBro(value as OverDto);
                    break;
                case FightCode.PASS_SRES:
                    passResponse((int) value);
                    break;
            }
        }

        private void passResponse(int _value)
        {
            switch (_value)
            {
                case 0:
                    Dispatch(AreaCode.UI, UIEvent.SHOW_DEAL_BUTTON, false);
                    break;
            }
        }

        /// <summary>
        /// 收到结束广播
        /// </summary>
        /// <param name="_overDto"></param>
        private void overBro(OverDto _overDto)
        {
            // 播放结果音效
            if (_overDto.WinUidList.Contains(Models.GameModel.Id))
            {
                Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_EFFECT_AUDIO, "Fight/MusicEx_Win");
            }
            else
            {
                Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_EFFECT_AUDIO, "Fight/MusicEx_Lose");
            }

            // 显示结束面板
            Dispatch(AreaCode.UI, UIEvent.SHOW_OVER_PANEL, _overDto);
        }

        /// <summary>
        /// 用户出牌服务器的相应处理
        /// </summary>
        /// <param name="result">-1 出牌失败</param>
        private void dealResponse(int result)
        {
            switch (result)
            {
                case -1:
                    // 出牌失败，提示用户牌太小，重新出
                    PromptMsg promptMsg = new PromptMsg("出的牌太小了", Color.red);

                    Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, promptMsg);

                    // 显示出牌按钮
                    Dispatch(AreaCode.UI, UIEvent.SHOW_DEAL_BUTTON, true);
                    break;
                case 0:
                    // 成功隐藏出牌按钮
                    Dispatch(AreaCode.UI, UIEvent.SHOW_DEAL_BUTTON, false);
                    break;
            }
        }

        /// <summary>
        /// 同步出牌
        /// </summary>
        /// <param name="dealDto"></param>
        private void DealBro(DealDto dealDto)
        {
            // 1.移除出完的手牌
            int eventCode = -1;

            if (dealDto.userid == Models.GameModel.MatchRoomDto.LeftId)
            {
                eventCode = CharacterEvent.REMOVE_LEFT_CARD;
            }
            else if (dealDto.userid == Models.GameModel.MatchRoomDto.RightId)
            {
                eventCode = CharacterEvent.REMOVE_RIGHT_CARD;
            }
            else if (dealDto.userid == Models.GameModel.UserDto.Id)
            {
                eventCode = CharacterEvent.REMOVE_MY_CARD;
            }

            Dispatch(AreaCode.CHARACTER, eventCode, dealDto.remainCardList);
            // 2.显示到桌面上
            Dispatch(AreaCode.CHARACTER, CharacterEvent.UPDATE_SHOW_DESK, dealDto.SelectCardLists);

            // 3.播放出牌音效
            playDealAudio(dealDto.type, dealDto.weight);
        }

        private void playDealAudio(int cardType, int wight)
        {
            String audioName = "Fight/";

            switch (cardType)
            {
                case CardType.NONE:
                    break;
                case CardType.SINGLE:
                    audioName += "Woman_" + wight;
                    break;
                case CardType.DOUBLE:
                    audioName += "Woman_dui" + wight / 2;
                    break;

                case CardType.STRAIGHT:
                    audioName += "Woman_shunzi";
                    break;

                case CardType.DOUBLE_STRAIGHT:
                    audioName += "Woman_liandui";
                    break;

                case CardType.TRIPLE_STRAIGHT:
                    audioName += "Woman_feiji";
                    break;

                case CardType.THREE:
                    audioName += "Woman_tuple" + wight / 3;
                    break;

                case CardType.THREE_ONE:
                    audioName += "Woman_sandaiyi";
                    break;

                case CardType.THREE_TWO:
                    audioName += "Woman_sandaiyidui";
                    break;

                case CardType.BOOM:
                    audioName += "Woman_zhadan";
                    break;

                case CardType.JOKER_BOOM:
                    audioName += "Woman_wangzha";
                    break;
            }

            Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_EFFECT_AUDIO, audioName);
        }

        private void TurnDealBro(int userId)
        {
            if (Models.GameModel.UserDto.Id == userId)
            {
                Dispatch(AreaCode.UI, UIEvent.SHOW_DEAL_BUTTON, true);
            }
        }

        /// <summary>
        /// 抢地主成功的处理
        /// </summary>
        private void grabLandLordBro(GrabDto grabDto)
        {
            // 更改UI的身份显示
            Dispatch(AreaCode.UI, UIEvent.PLAYER_CHANGE_IDENTITY, grabDto);

            // 播放抢地主的声音
            Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_EFFECT_AUDIO, "Fight/Woman_Order");

            // 显示三张底牌
            Dispatch(AreaCode.UI, UIEvent.SET_TABLE_CARDS, grabDto.tableCardList);

            // 将底牌给地主玩家 添加牌
            int eventCode = -1;
            if (Models.GameModel.MatchRoomDto.LeftId == grabDto.userId)
            {
                eventCode = CharacterEvent.ADD_LEFT_CARD;
            }
            else if (Models.GameModel.MatchRoomDto.RightId == grabDto.userId)
            {
                eventCode = CharacterEvent.ADD_RIGHT_CARD;
            }
            else if (Models.GameModel.UserDto.Id == grabDto.userId)
            {
                eventCode = CharacterEvent.ADD_MY_CARD;
            }

            Dispatch(AreaCode.CHARACTER, eventCode, grabDto);
        }

        /// <summary>
        /// 转换抢地主
        /// </summary>
        /// <param name="userId"></param>
        private void turnGrabBro(int userId)
        {
            if (isFirstGrab)
            {
                isFirstGrab = false;
            }
            else
            {
                // 播放声音
                Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_EFFECT_AUDIO, "Fight/Woman_NoOrder");
            }

            // 如果是自己就显示 抢地主和不抢的按钮
            if (userId == Models.GameModel.UserDto.Id)
            {
                Dispatch(AreaCode.UI, UIEvent.SHOW_CRAB_BUTTON, true);
            }
        }


        /// <summary>
        /// 获取到卡牌的处理
        /// </summary>
        /// <param name="cardDtoList"></param>
        private void GetCards(List<CardDto> cardDtoList)
        {
            // 给玩家创建牌的对象
            Dispatch(AreaCode.CHARACTER, CharacterEvent.INIT_MY_CARD, cardDtoList);
            Dispatch(AreaCode.CHARACTER, CharacterEvent.INIT_LEFT_CARD, null);
            Dispatch(AreaCode.CHARACTER, CharacterEvent.INIT_RIGHT_CARD, null);

            // 设置显示倍数默认为1
            Dispatch(AreaCode.UI, UIEvent.CHANGE_MUTIPLE, 1);
        }
    }
}